/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
import Taro, { Component } from '@tarojs/taro'

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast, Loading, Modal, ActionSheet } from '../../../library/func';
import Icon from '../../../project/style/icon';
import Http from '../../../project/func/http/http'
import { S_IRWXG } from 'constants';


/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class ProductList extends Component {

	/*------------------------------------------------------------------------------------
	 * 初始化
	 *-----------------------------------------------------------------------------------*/
	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
			
		}
	}

	/*------------------------------------------------------------------------------------
	 * 生命周期
	 *-----------------------------------------------------------------------------------*/
	// didMount
	componentDidMount() {
		console.log('this.props.data=========',this.props.data)
	}
	
	/*------------------------------------------------------------------------------------
	 * 事件处理
	 *-----------------------------------------------------------------------------------*/

	// 用户取消订单操作
	cancelOrder = (id, e) => {
		Modal.confirm('确认执行此操作吗？', '取消订单', ()=> {

			Http.post({
				url:'order/logic/cancel.php',
				data:{
					id: id
				},
				success:(data)=>{

					Toast.success('取消订单成功！')
					setTimeout(()=>{
						wx.navigateTo({
							url: '/screen/user/order/list?status=已取消'
						});
					},1500)

				},
			})

		},()=>{

		})
	}

	// 用户完成订单操作
	onOk = (id, e) => {
		Modal.confirm('确认执行此操作吗？', '确认收货', ()=> {

			Http.post({
				url:'order/logic/complete.php',
				data:{
					id: id
				},
				success:(data)=>{
					Toast.success('确认收货成功！')
					setTimeout(()=>{
						wx.navigateTo({
							url: '/screen/user/order/list?status=已完成'
						});
					},1500)
				},
			})

		},()=>{

		})
	}

	// 用户支付未付款订单
	onPay = (item, e) => {

		let orderid = item.set_orderid
		let orderid_new = orderid + '-' + (new Date()).getTime();
		let total_fee = item.set_amount * 100;

		// 从localStorage 查询 openid
		wx.getStorage({
			key: 'openid',
			success(res) {
				console.log('456============',res)
				let openid = res.data;
				wx.request({
					url: 'https://activepage.sgboke.com/pay/wepay/jsapi.php',
					method: 'POST',
					data: {
						key: 'rushbuy',
						orderid: orderid_new,
						total_fee: total_fee,
						openid: openid
					},
					header: {
						'content-type': "application/x-www-form-urlencoded",
					},
					success(res) {
						console.log('789==============',res)

						// 取参数
						let nonceStr = res.data.nonceStr;
						let prepay_id = res.data.package;
						let paySign = res.data.paySign;
						let timeStamp = res.data.timeStamp;

						// 调起支付 
						wx.requestPayment({
							timeStamp: timeStamp.toString(),
							nonceStr: nonceStr,
							package: prepay_id,
							signType: 'MD5',
							paySign: paySign,

							// 支付成功
							success: function(res) {
								console.log('成功');
								console.log(res);

								/**
								 * 支付成功，调整订单状态，并跳转到未发货页面
								 */
								Http.post({
									url: 'order/logic/ok.php',
									data: {
										orderid: orderid
									},

									// 调整订单状态成功
									success(res) {
										wx.redirectTo({
										    url: '/screen/user/order/list?status=未发货'
										});
									},
									code(err) {
										console.log('支付失败：调整订单状态，逻辑失败！');
										console.log(err);
									},
									server(err) {
										console.log('支付失败：调整订单状态，服务器失败！');
										console.log(err);
									},
									net(err) {
										console.log('支付失败：调整订单状态，网络失败！');
										console.log(err);
									}
								});
							},

							// 支付失败
							fail: function(err) {
								console.log('失败');
								console.log(err);
								/**
								 * 支付失败，跳转到未支付页
								 */
								wx.redirectTo({
								    url: '/screen/user/order/list?status=未付款'
								});
							}
						})
						// console.log(res);
					},
					fail(err) {
						console.log('支付失败：网络失败！');
					}
				})
			}
		})

	}
    // onNavigate
    onRedirect = (url, e)=> {
        wx.redirectTo({
            url: url
        })
    }

    // onNavigate
    onNavigate = (url, e)=> {
		wx.navigateTo({
			url: url
		})
	}
	/*------------------------------------------------------------------------------------
	 * 自定义方法
	 *-----------------------------------------------------------------------------------*/

	/*------------------------------------------------------------------------------------
	 * 渲染
	 *-----------------------------------------------------------------------------------*/
	render () {
		return (
			<CColumn ma='20'>
				{this.props.data.map((item,index) => {
					let name = item.object.set_order_name
					if(name.length > 28){
						name = name.substr(0, 28) + '...'
					}
					return (
						<CColumn bg='#fff' mb='20' ra='20'>
							{/* 店铺名称 */}
							<CBetween pr='20' >
								<CLine px='20' py='30'>
									<CSvg icon={Icon.store} w='33' h='31'></CSvg>
									<CText size='28' pl='16'>每日一达</CText>
								</CLine>
								<CHand size='28' color='#df3641'>{item.enum_status}</CHand>
							</CBetween>

							{/* 商品信息 */}
							<CLine bg='#f9f9f9' pa='16' y='center' bb='2 solid #f6f6f6' onClick={this.onNavigate.bind(this, '/screen/user/order/detail?id=' + item.id)}>
								<CImage src={'http://image.cdn.hesq.com.cn/rushbuy/product/' + item.object.set_order_image} w='150' h='150'/>
								<CColumn pl='20'>
									<CBetween y='top'>
										<CHand size='28' color='#252525' class='nameText'>{name}</CHand>
										<CHand size='28' pl='30' pr='10'  color='#252525'>￥{item.set_amount}</CHand>
									</CBetween>
									<CLine pt='10' pr='10' x='right'>
										<CHand size='28' color='#aaa'>x{item.count_product}</CHand>
									</CLine>
								</CColumn>
							</CLine>

							{/* 订单信息底部部分 */}
							<CLine px='30' pt='30' x='right'  >
								<CHand size='30'>共计 {item.count_product} 件商品&nbsp;&nbsp;&nbsp;实付:</CHand>
								<CHand size='34' color='#cc2d2e' bold>￥{item.set_amount}</CHand>
							</CLine>

							{/* 按钮 */}
							<CLine pa='30' x='right'>
								{/* 已完成显示 */}
								{item.enum_status == '已完成' && 
									<CButton ba='2 solid #cb2829' bg='#cb2829' ml='20' ra='100' py='6' px='20'color='#fff' style='font-size:28rpx;' onClick={this.onNavigate.bind(this, '/screen/user/order/comment?oiid=' + item.id)}>评论订单</CButton>
								}

								{/* 已发货显示 */}
								{item.enum_status == '已发货' && 
									<CButton ba='2 solid #cb2829' bg='#cb2829' ml='20' ra='100' py='6' px='20'color='#fff' style='font-size:28rpx;' onClick={this.onOk.bind(this, item.id)}>确认收货</CButton>	
								}

								{/* 未付款显示 */}
								{item.enum_status == '未付款' && 
									<CHand>
										<CButton style='font-size:28rpx;' ba='2 solid #cb2829' ra='100' py='6' px='20'color='#cb2829' onClick={this.cancelOrder.bind(this, item.id)}>取消订单</CButton>
										<CButton ba='2 solid #cb2829' bg='#cb2829' ml='20' ra='100' py='6' px='50'color='#fff' style='font-size:28rpx;' onClick={this.onPay.bind(this, item)}>付款</CButton>
									</CHand>
								}

								{/* 未发货显示 */}
								{item.enum_status == '未发货' && 
									<CHand>
										<CButton style='font-size:28rpx;' ba='2 solid #cb2829' bg='#cb2829' ra='100' py='6' px='20'color='#fff' onClick={this.onNavigate.bind(this, '/screen/user/order/detail?id=' + item.id)}>查看订单</CButton>
									</CHand>
								}

								{/* 已取消显示 */}
								{item.enum_status == '已取消' && 
									<CHand>
										<CButton style='font-size:28rpx;' ba='2 solid #cb2829' bg='#cb2829' ra='100' py='6' px='20'color='#fff' onClick={this.onNavigate.bind(this, '/screen/user/order/detail?id=' + item.id)}>查看订单</CButton>
									</CHand>
								}
							</CLine>

						</CColumn>
					)
				})}
            </CColumn>
		)
	}
}


